//
//  UIViewController+Extension.m
//  OMTCoreKit
//
//  Created by 昭荣伊 on 2018/7/27.
//

#import "UIViewController+Extension.h"

@implementation UIViewController (Extension)

+ (UIViewController *)omt_currentViewController{
    UIViewController * vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (true)
    {
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}

+ (UINavigationController *)omt_currentNavigationController {
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    if (!rootViewController) {
        return nil;
    }
    
    UINavigationController *navController = nil;
    
    while (true) {
        if ([rootViewController isKindOfClass:[UINavigationController class]]) {
            navController = (UINavigationController *)rootViewController;
            
            break;
        } else {
            if ([rootViewController isKindOfClass:[UITabBarController class]]) {
                UITabBarController *tabbar = (UITabBarController *)rootViewController;
                if ([tabbar.selectedViewController isKindOfClass:[UINavigationController class]]) {
                    navController = (UINavigationController *)tabbar.selectedViewController;
                    
                    break ;
                }
            }
        }
    }
    
    return navController;
}

@end
